import { createRouter, createWebHistory } from 'vue-router'
import store from '../store'
//导入登录注册
import LoginPage from '../front page/LoginPage.vue'
import RegisterPage from '../front page/RegisterPage.vue'
// 导入主页模块
import Index from '../front page/index/index.vue'
import ScenicMain from '../front page/index/scenic/ScenicMain.vue'
import StrategyMain from '../front page/index/strategy/StrategyMain.vue'
import UStrategyDetail from '../front page/index/strategy/StrategyDetail.vue'
import Rank from '../front page/index/rank/Rank.vue'
import GroupTourMain from '../front page/index/grouptour/GroupTourMain.vue'
// 导入用户模块
import UserFrame from '../front page/index/user/UserFrame.vue'


//导入主页组件
import HomePage from '../front page/indexFrame.vue'
// 导入主布局组件
import MainLayout from '../admin/MainLayout.vue'

// 导入各页面组件（根据新目录结构调整路径）
// 数据统计模块
import Main from '../admin/Data/Main.vue'
import Money from '../admin/Data/Money.vue'

// 用户管理模块
import UserList from '../admin/InfoMgmt/UserMgmt/UserList.vue'
import FollowList from '../admin/InfoMgmt/UserMgmt/FollowList.vue'
import StrategyList from '../admin/InfoMgmt/UserMgmt/StrategyList.vue'

// 机构管理模块
import AgencyList from '../admin/InfoMgmt/AgencyMgmt/AgencyList.vue'
import AgencyAudit from '../admin/InfoMgmt/AgencyMgmt/AgencyAudit.vue'

//酒店管理模块
import HotelEdit from '../admin/InfoMgmt/HotelMgmt/HotelEdit.vue'
import HotelList from '../admin/InfoMgmt/HotelMgmt/HotelList.vue'

// 景点管理模块
import ScenicEdit from '../admin/InfoMgmt/ScenicMgmt/ScenicEdit.vue'
import ScenicList from '../admin/InfoMgmt/ScenicMgmt/ScenicList.vue'

// 旅游管理模块
import TravelAudit from '../admin/InfoMgmt/TravelMgmt/TravelAudit.vue'
import TravelList from '../admin/InfoMgmt/TravelMgmt/TravelList.vue'
import TravelActive from '../admin/InfoMgmt/TravelMgmt/TravelActive.vue'
import TravelActiveDetail from '../admin/InfoMgmt/TravelMgmt/TravelActiveDetail.vue'

// 评论管理模块
import ScenicComment from '../admin/CommentMgmt/ScenicComment.vue'
import StrategyComment from '../admin/CommentMgmt/StrategyComment.vue'

// 收藏管理模块
import HotelCollect from '../admin/CollecMgmt/HotelCollec.vue'
import ScenicCollect from '../admin/CollecMgmt/ScenicCollec.vue'
import StrategyCollect from '../admin/CollecMgmt/StrategyCollec.vue'
import TravelCollect from '../admin/CollecMgmt/TravelCollec.vue'

// 点赞管理模块
import ScenicLike from '../admin/LikeMgmt/ScenicLike.vue'
import StrategyLike from '../admin/LikeMgmt/StrategyLike.vue'

//登录日志模块
import SystemLogs from '../admin/LogsPage.vue'

//旅行社模块
import AgencyMain from '../front page/Agency/AgencyMain.vue'
import RegDetail from '../front page/Agency/RegDetail.vue'
import RegList from '../front page/Agency/RegList.vue'
import GroupTourPost from '../front page/Agency/GroupTourPost.vue'
import GroupTourSend from '../front page/Agency/GroupTourSend.vue'
//框架模块
import AgencyFrame from '../front page/AgencyFrame.vue'
import GroupTourAudit from '../front page/Agency/GroupTourAudit.vue'

// 导入用户模块子页面组件
import UserMain from '../front page/index/user/UserMain.vue'
import USciencLike from '../front page/index/user/like/SciencLike.vue'
import UStrategyLike from '../front page/index/user/Like/StrategyLike.vue'
import UHotelCollec from '../front page/index/user/Collec/HotelCollec.vue'
import USciencCollec from '../front page/index/user/Collec/SciencCollec.vue'
import UStrategyCollec from '../front page/index/user/Collec/StrategyCollec.vue'
import UTravelAgencyCollec from '../front page/index/user/Collec/TravelAgencyCollec.vue'
import UScienceCom from '../front page/index/user/Comment/ScienceCom.vue'
import UStrategyCom from '../front page/index/user/Comment/StrategyCom.vue'
import UMyStrategy from '../front page/index/user/Strategy/MyStrategy.vue'
import UPutStrategy from '../front page/index/user/Strategy/PutStrategy.vue'
import UGetLikeInfo from '../front page/index/user/Info/GetLikeInfo.vue'
import UGetFollowInfo from '../front page/index/user/Info/GetFollowInfo.vue'
import UGetComInfo from '../front page/index/user/Info/GetComInfo.vue'
import UGetCollecInfo from '../front page/index/user/Info/GetCollecInfo.vue'

const routes = [
  //登录注册
  { path: '/loginpage', component: LoginPage, name: 'LoginPage' },
  { path: '/registerpage', component: RegisterPage, name: 'RegisterPage' },

  // 主页模块（前端用户界面）
  {
    path: '/homepage',
    component: HomePage,
    name: 'HomePage',
    children: [
      { path: '/', component: Index, name: 'Home' }, // 默认首页
      { path: 'scenic', component: ScenicMain, name: 'Scenic' },
      {
        path: 'scenic/:id',
        name: 'ScenicDetail',
        component: () => import('@/front page/index/scenic/ScenicDetail.vue'),
        props: true // This allows `scenicSpotId` to be passed as a prop to ScenicDetail.vue
      },
      { path: 'strategy', component: StrategyMain, name: 'Strategy' },
      {
        path: 'strategy/detail/:id',
        component: UStrategyDetail,
        name: 'UStrategyDetail',
        props: true
      },
      { path: 'rank', component: Rank, name: 'Rank' },
      { path: 'group-tour', component: GroupTourMain, name: 'GroupTour' },
      { path: 'user/:id', component: UserMain, name: 'UserMainProfile', props: true },
      {
        path: 'agencydetail/:id',
        name: 'AgencyDetail',
        component: () => import('@/front page/Agency/AgencyDetail.vue'),
        props: true
      },
      {
        path: 'group-tour/detail/:id',
        name: 'GroupTourDetail',
        component: () => import('@/front page/index/grouptour/GroupTourDetailPage.vue'),
        props: true
      }
    ]
  },
{
  path: '/user/profile/:id',
  name: 'UserProfileView',
  component: () => import('@/front page/index/user/UserProfileView.vue'),
  props: true
},

  {
    path: '/userframe',
    component: UserFrame,
    name: 'UserFrame',
    children: [
      { path: 'usermain', component: UserMain, name: 'UserMain' },
      // 我的点赞
      { path: 'like/strategy', component: UStrategyLike, name: 'UStrategyLike' },
      { path: 'like/scenic', component: USciencLike, name: 'USciencLike' },
      // 我的收藏
      { path: 'collec/strategy', component: UStrategyCollec, name: 'UStrategyCollec' },
      { path: 'collec/scenic', component: USciencCollec, name: 'USciencCollec' },
      { path: 'collec/hotel', component: UHotelCollec, name: 'UHotelCollec' },
      { path: 'collec/grouptour', component: UTravelAgencyCollec, name: 'UTravelAgencyCollec' },
      // 我的评论
      { path: 'comment/strategy', component: UStrategyCom, name: 'UStrategyCom' },
      { path: 'comment/scenic', component: UScienceCom, name: 'UScienceCom' },
      // 我的攻略
      { path: 'strategy/new', component: UPutStrategy, name: 'UPutStrategy' },
      { path: 'strategy/published', component: UMyStrategy, name: 'UMyStrategy' },
      // 攻略详情
      {
        path: '/userframe/strategy/detail/:id',
        name: 'StrategyDetail',
        component: () => import('@/front page/index/Strategy/StrategyDetail.vue')
      },
      // 编辑攻略
      {
        path: '/userframe/strategy/edit/:id',
        name: 'EditStrategy',
        component: () => import('@/front page/index/user/Strategy/EditStrategy.vue')
      },
      // 消息通知
      { path: 'notifications/likes', component: UGetLikeInfo, name: 'UGetLikeInfo' },
      { path: 'notifications/follows', component: UGetFollowInfo, name: 'UGetFollowInfo' },
      { path: 'notifications/comments', component: UGetComInfo, name: 'UGetComInfo' },
      { path: 'notifications/collections', component: UGetCollecInfo, name: 'UGetCollecInfo' },
    ]
  },

  // 管理后台模块
  {
    path: '/admin',
    component: MainLayout,
    redirect: '/admin/main', // 默认重定向到管理首页
    children: [
      // 数据统计
      { path: '/main', component: Main, name: 'Main' },
      { path: '/money', component: Money, name: 'Money' },

      // 用户管理
      { path: '/user/list', component: UserList, name: 'UserList' },
      { path: '/user/follows', component: FollowList, name: 'FollowList' },
      { path: '/user/strategies', component: StrategyList, name: 'StrategyList' },
      { path: '/user/strategy-detail/:id', component: () => import('../admin/InfoMgmt/UserMgmt/StrategyDetail.vue'), name: 'StrategyDetail', props: true },

      // 酒店管理
      { path: '/hotel/edit', component: HotelEdit, name: 'HotelEdit' },
      { path: '/hotel/list', component: HotelList, name: 'HotelList' },

      // 机构管理
      { path: '/agency/list', component: AgencyList, name: 'AgencyList' },
      { path: '/agency/audit/:id', component: AgencyAudit, name: 'AgencyAudit', pros: true },

      // 景点管理
      { path: '/scenic/edit', component: ScenicEdit, name: 'ScenicEdit' },
      { path: '/scenic/list', component: ScenicList, name: 'ScenicList' },

      // 旅游管理
      { path: '/travel-audit/:id', component: TravelAudit, name: 'TravelAudit', props: true },
      { path: '/travel/list', component: TravelList, name: 'TravelList' },
      { path: '/travel/active', component: TravelActive, name: 'TravelActive' },
      { path: '/travel/active-detail/:id', component: TravelActiveDetail, name: 'TravelActiveDetail', props: true },

      // 评论管理
      { path: '/comment/scenic', component: ScenicComment, name: 'ScenicComment' },
      { path: '/comment/strategy', component: StrategyComment, name: 'StrategyComment' },

      // 收藏管理
      { path: '/collect/hotel', component: HotelCollect, name: 'HotelCollect' },
      { path: '/collect/scenic', component: ScenicCollect, name: 'ScenicCollect' },
      { path: '/collect/strategy', component: StrategyCollect, name: 'StrategyCollect' },
      { path: '/collect/travel', component: TravelCollect, name: 'TravelCollect' },

      // 点赞管理
      { path: '/like/scenic', component: ScenicLike, name: 'ScenicLike' },
      { path: '/like/strategy', component: StrategyLike, name: 'StrategyLike' },
      { path: '/system/logs', component: SystemLogs, name: 'SystemLogs' }
    ]

  },

  // 旅行社模块
  {
    path: '/agencyframe',
    component: AgencyFrame,
    redirect: '/agencyframe/agencymain',
    children: [
      {
        path: 'agencymain',
        component: AgencyMain,
        name: 'AgencyMain'
      },
      {
        path: 'regdetail/:id',
        component: RegDetail,
        name: 'RegDetail',
        props: true
      },
      { path: 'reglist', component: RegList, name: 'RegList' },
      { path: 'grouptourpost', component: GroupTourPost, name: 'GroupTourPost' },
      { path: 'grouptoursend', component: GroupTourSend, name: 'GroupTourSend' },
      {
        path: 'grouptouraudit/:id',
        component: GroupTourAudit,
        name: 'GroupTourAudit',
        props: true
      }
    ]
  }


]

const router = createRouter({
  history: createWebHistory('/'), // 修改这里
  routes
})

// 导入 ElMessage
import { ElMessage } from 'element-plus';

router.beforeEach((to, from, next) => {
  const isAuthenticated = store.state.isAuthenticated
  const requiredRole = to.meta?.role

  if (to.meta?.requiresAuth && !isAuthenticated) {
    next('/login')
  } else if (requiredRole && store.state.user?.role !== requiredRole) {
    next('/unauthorized')
  } else {
    next()
  }
})

// 在每次路由跳转完成后，关闭所有 ElMessage 提示
router.afterEach(() => {
  ElMessage.closeAll();
});

export default router